package com.keyroy.game.puzzle_rpg.dialogs;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.wb.swt.ResourceManageX;

import com.keyroy.game.puzzle_rpg.models.environment.Project;
import com.keyroy.game.puzzle_rpg.models.interfaces.EventListener;
import com.keyroy.game.puzzle_rpg.models.skill.Skill;
import com.keyroy.game.puzzle_rpg.models.skill.SkillFolder;
import com.keyroy.game.puzzle_rpg.models.skill.SkillSet;
import com.keyroy.game.puzzle_rpg.utils.StringUtil;

public class SkillSelector extends Shell {
	private static final String FOLDER_TAG = "folder";
	private static final String SKILL_TAG = "skill";
	private Tree tree;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			SkillSelector shell = new SkillSelector(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public SkillSelector(Display display, final EventListener<Skill> listener) {
		super(display, SWT.DIALOG_TRIM);
		setLayout(new FormLayout());

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (listener != null && tree.getSelection() != null) {
					TreeItem[] treeItems = tree.getSelection();
					if (treeItems.length > 0) {
						TreeItem treeItem = treeItems[0];
						Skill skill = (Skill) treeItem.getData(SKILL_TAG);
						if (skill != null) {
							listener.onEvent(skill);
						}
					}
				}
				dispose();
			}
		});
		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.left = new FormAttachment(100, -141);
		fd_btnNewButton.bottom = new FormAttachment(100, -10);
		fd_btnNewButton.right = new FormAttachment(100, -10);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("\u5B8C\u6210");

		tree = new Tree(this, SWT.BORDER);
		FormData fd_tree = new FormData();
		fd_tree.bottom = new FormAttachment(btnNewButton, -6);
		fd_tree.right = new FormAttachment(btnNewButton, 0, SWT.RIGHT);
		fd_tree.top = new FormAttachment(0, 10);
		fd_tree.left = new FormAttachment(0, 10);
		tree.setLayoutData(fd_tree);
		createContents();

	}

	public void reset() {
		Project project = Project.getProject();
		if (project != null) {
			SkillSet skillSet = project.getSkillSet();
			List<SkillFolder> skillFolders = skillSet.getSkillFolders();
			for (int i = 0; i < skillFolders.size(); i++) {
				SkillFolder skillFolder = skillFolders.get(i);
				TreeItem folderTreeItem = new TreeItem(tree, SWT.NONE);
				folderTreeItem.setText(StringUtil.stringOf(skillFolder.name));
				folderTreeItem.setImage(ResourceManageX.loadImage(skillFolder.icon, 16, 16));

				folderTreeItem.setData(FOLDER_TAG, skillFolder);

				for (int j = 0; j < skillFolder.skills.size(); j++) {
					Skill skill = skillFolder.skills.get(j);
					TreeItem skillTreeItem = new TreeItem(folderTreeItem, SWT.NONE);
					skillTreeItem.setImage(ResourceManageX.loadImage(skill.icon, 16, 16));
					skillTreeItem.setText(StringUtil.stringOf(skill.name));
					skillTreeItem.setChecked(false);

					skillTreeItem.setData(FOLDER_TAG, skillFolder);
					skillTreeItem.setData(SKILL_TAG, skill);
				}

				folderTreeItem.setExpanded(true);
			}
		}
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u6280\u80FD\u9009\u62E9");
		setSize(450, 300);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
